package com.designpattern.design.iterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IteratorTest {
	public static void main(String[] args) {
		List<College> list=new ArrayList<>();
		ComputerCollege computer =new ComputerCollege();
		computer.addDepartMent("java", "java");
		computer.addDepartMent("python", "python");
		computer.addDepartMent("c++", "c++");
		computer.addDepartMent("go", "go");
		list.add(computer);
		EduCollege edu=new EduCollege();
		edu.addDepartMent("教育学", "教育学");
		edu.addDepartMent("心理学", "心理学");
		list.add(edu);
		OutPutImpl output=new OutPutImpl(list);
		output.printCollege();
	}
}

/**
 * 以集合的方式存储元素
 * @author zhangyang
 *
 */
class ArrayListIterator implements Iterator<DepartMent>{

	List<DepartMent> departMentList;
	int index=-1;
	
	
	public ArrayListIterator(List<DepartMent> departMentList) {
		super();
		this.departMentList = departMentList;
	}

	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		if(index >= departMentList.size() - 1) {
			return false;
		} else {
			index += 1;
			return true;
		}
	}

	@Override
	public DepartMent next() {
		// TODO Auto-generated method stub
		return departMentList.get(index);
	}

	@Override
	public void remove() {
		// TODO Auto-generated method stub
		departMentList.remove(index);
	}
	
}

/**
 * 数组的方式存储对象
 * @author zhangyang
 *
 */
class StringArrayIterator implements Iterator<DepartMent>{

	DepartMent[] DpartArray;
	int index=0;
	
	
	public StringArrayIterator(DepartMent[] dpartArray) {
		super();
		DpartArray = dpartArray;
	}


	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		if(index>=DpartArray.length || DpartArray[index] == null) {
			return false;
		}else {
			return true;
		}
		
	}


	@Override
	public DepartMent next() {
		// TODO Auto-generated method stub
		/**
		 * 先赋值 然后再增加下标
		 */
		DepartMent departMent = DpartArray[index];
		index+=1;
		return departMent;
	}

	@Override
	public void remove() {
		// TODO Auto-generated method stub
		
	}
	
}

class DepartMent{
	String name;
	String desc;
	public DepartMent(String name, String desc) {
		super();
		this.name = name;
		this.desc = desc;
	}
	
}

interface College{
	public String getName();
	public void addDepartMent(String name,String desc);
	/**
	 * 核心 返回一个迭代器
	 * 
	 */
	public Iterator getCollegeIterator();
}

class ComputerCollege implements College{

	DepartMent[] departments;
	int index=0;
	
	
	public ComputerCollege() {
		super();
		departments=new DepartMent[10];
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "计算机学院";
	}

	@Override
	public void addDepartMent(String name,String desc) {
		// TODO Auto-generated method stub
		DepartMent department=new DepartMent(name,desc);
		departments[index]=department;
		index+=1;
	}

	/* (non-Javadoc)
	 * @see com.design.iterator.College#getCollegeIterator()
	 */
	@Override
	public Iterator getCollegeIterator() {
		// TODO Auto-generated method stub
		return new StringArrayIterator(departments);
	}
	
}

class EduCollege implements College{
	
	List<DepartMent> list=new ArrayList<>();
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "教育学院";
	}

	@Override
	public void addDepartMent(String name, String desc) {
		// TODO Auto-generated method stub
		DepartMent department=new DepartMent(name,desc);
		list.add(department);
	}

	@Override
	public Iterator getCollegeIterator() {
		// TODO Auto-generated method stub
		return new ArrayListIterator(list);
	}
	
}


class OutPutImpl {
	
	//学院集合
	List<College> collegeList;

	public OutPutImpl(List<College> collegeList) {
		
		this.collegeList = collegeList;
	}
	//遍历所有学院,然后调用printDepartment 输出各个学院的系
	public void printCollege() {
		
		//从collegeList 取出所有学院, Java 中的 List 已经实现Iterator
		Iterator<College> iterator = collegeList.iterator();
		
		while(iterator.hasNext()) {
			//取出一个学院
			College college = iterator.next();
			System.out.println("=== "+college.getName() +"=====" );
			printDepartment(college.getCollegeIterator()); //得到对应迭代器
		}
	}
	
	
	//输出 学院输出 系
	
	public void printDepartment(Iterator iterator) {
		while(iterator.hasNext()) {
			DepartMent d = (DepartMent)iterator.next();
			System.out.println(d.name);
		}
	}
	
}
